﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.IO;
using System.Collections.Generic;
using System.Web.Script.Serialization;
using Chinook.Core;
using Chinook.Document;
using Base.Common;
using MongoDB;
using MongoDB.Driver;

namespace Chinook.Web.MongoDb
{
    public partial class FileUploadHandler : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {

            //string helloWorld = (string) ExpressionEvaluator.GetValue(null, "'Hello World'");
            try
            {
                // System.Threading.Thread.Sleep(1000);
                JavaScriptSerializer js = new JavaScriptSerializer();
                Chinook.Core.DomainModel.Document doc = js.Deserialize<Chinook.Core.DomainModel.Document>(Request.Form["request"]);
                //Chinook.Core.DomainModel.Employee emp = new Chinook.Core.DomainModel.Employee();
               
                HttpPostedFile file = Request.Files["Filedata"];
                Chinook.Document.DocumentDao docDao = new Chinook.Document.DocumentDao();
                docDao.AddFile(file.InputStream, file.FileName);
                //byte[] fs = docDao.FindByName("kothari.png", 0);
                //docDao.AddFile(file.InputStream, file.FileName);
                // using (var fs = new FileStream(newFileName, FileMode.Open))
                //{
                //    var server = MongoServer.Create("mongodb://localhost:27017");
                //    var database = server.GetDatabase("blog");
                //    var gridFsInfo = database.GridFS.Upload(fs, newFileName);



                //}
                //doc.BinaryContent = Base.Utils.CommonUtils.GetFileAsByteArray(file);

                //file.SaveAs(Server.MapPath("~/upload/" + file.FileName));
            }
            catch (Exception ex)
            {
                // If any kind of error occurs return a 500 Internal Server error
                Response.StatusCode = 500;
                Response.Write("An error occured");
                Response.End();
            }
            finally
            {
                Response.End();
            }

        }
    }
}
